<?php
header ('Content-type: text/html; charset=utf-8');
require_once realpath (dirname (__FILE__) . '//..//pages.php');
require_once CLASS_USER;
require_once INC_FUNCTIONS;
require_once INC_MESSAGES;
require_once MDL_ACTIONS;
require_once MDL_TABLES;

authorize ();
if (!isset ($request) || empty ($request)) {
    notify ('No request set!');
    exit ();
}
if (!isset ($result) || empty ($result)) {
    notify ('No data to load!');
    exit ();
}
if (!isset ($table) || empty ($table)) {
    notify ('No table to load!');
    exit ();
}

$db = openConnection ();
$actions = getActions ();
$fields = $db->getFields ($result);
$tables = getTables ();
if (isset ($_SESSION[KEY_SESSION_USER])) {
    $user = $_SESSION[KEY_SESSION_USER];
} else {
    notify(MSG_NOT_AUTHORIZED);
    exit ();
}

/* Preparing previous and next tables */
$keys = array_keys ($tables);
$key = array_search ($table, $keys);
if ($key || $key == 0) {
    if (isset ($keys[$key - 1])) {
        $l = $keys[$key - 1];
        $l_label = $tables[$keys[$key - 1]];
    } else {
        $l = null;
    }
    if (isset ($keys[$key + 1])) {
        $r = $keys[$key + 1];
        $r_label = $tables[$keys[$key + 1]];
    } else {
        $r = null;
    }
} else {
    $l = null;
    $r = null;
}
/**/

if (isset ($data['order'])) {
    $order = $data['order'];
} else {
    $order = null;
}
if (isset ($data['order_by'])) {
    $order_by = urldecode ($data['order_by']);
} else {
    $order_by = null;
}
?>
<html>
    <?php echo head (PAGE_TITLE, array (HTTP_CSS_LIST, HTTP_CSS_MENU, HTTP_CSS_STYLE), null, '<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />'); ?>
    <body>
        <?php
        require_once VW_MENU;
        ?>
        <h1><?php echo $tables[$table]; ?></h1>
        <table cellspacing="0" id="list" >
            <tr>
                <?php
                foreach ($fields as $field) :
                    if (substr ($field->name, 0, 2) != 'ID') :
                        /* For order */
                        $ref = HTTP_CTRL_MAIN . "?request=$request&table=$table&order_by=" . urlencode ($field->name) . "&order=";
                        if ($order_by == $field->name) {
                            switch ($order) {
                                case 'asc' :
                                    $ref .= 'desc';
                                    break;
                                case 'desc' :
                                default :
                                    $ref .= 'asc';
                                    break;
                            }
                        } else {
                            $ref .= 'asc';
                        }
                        /**/
                        ?>
                        <th>
                            <a href="<?php echo HTTP_CTRL_MAIN . "?request=$request&table=$table"; ?>" title="Ripristina l&apos;ordine predefinito">
                                <?php
                                $title = "Ordinato per $field->name";
                                if ($order_by == $field->name) {
                                    if ($order == 'asc') {
                                        $title .= ' (Crescente).';
                                        ?>
                                        <div id="up-triangle"></div>
                                        <?php
                                    } else if ($order == 'desc') {
                                        $title.= ' (Decrescente).';
                                        ?>
                                        <div id="down-triangle"></div>
                                        <?php
                                    } else {
                                        $title.= ' (Decrescente)';
                                        ?>
                                        <div id="down-triangle"></div>  
                                        <?
                                    }
                                }?></a>
                            <a href="<?php echo $ref; ?>" title="<?php echo $title . " \nClicca per ordinare in un&apos; altro modo."; ?>"><?php echo $field->name; ?></a>
                        </th>
                    <?php endif; ?>
                <?php endforeach; ?>
                <th></th>
                <th></th>
            </tr>
            <?php while ($row = $db->fetchAssoc ($result)) : ?>
                <tr>
                    <?php foreach ($row as $key => $value) : if (substr ($key, 0, 2) != 'ID') : ?>
                            <td>
                                <?php
                                echo $value;
                            endif;
                            ?>
                        </td>
                        <?php
                    endforeach;
                    $IDs = '';
                    foreach ($row as $key => $value) {
                        if (substr ($key, 0, 2) == 'ID') {
                            $IDs .= "$key=$value&";
                        }
                    }
                    $IDs = substr($IDs, 0, strlen($IDs) - 1);
                    if ($request == 'edit' || $request == 'view' && $table != 'permesso' && $user->can('edit', $table)) :
                        ?>
                        <th>
                            <a href="<?php echo HTTP_CTRL_MAIN . "?$IDs&table=$table"; ?>&request=edit"><img alt="<?php echo $actions['edit']; ?>" src="<?php echo HTTP_IMG_EDIT; ?>" title="<?php echo $actions['edit']; ?>" /></a>
                        </th>
                        <?php
                    endif;
                    if ($request == 'delete' || $request == 'view' && $user->can('delete', $table)) :
                        ?>
                        <th>
                            <a href="<?php echo HTTP_CTRL_MAIN . "?$IDs&table=$table"; ?>&request=delete"><img alt="<?php echo $actions['delete']; ?>" src="<?php echo HTTP_IMG_DELETE; ?>" title="<?php echo $actions['delete']; ?>" /></a>
                        </th>
                    <?php
                    endif;
                    if ($table != 'permesso' && $user->can('view', $table)):
                        ?>
                        <th>
                            <a href="<?php echo HTTP_CTRL_MAIN . '?values=' . $row['ID'] . "&table=$table"; ?>&request=view"><img alt="<?php echo $actions['view']; ?>" src="<?php echo HTTP_IMG_VIEW; ?>" title="<?php echo $actions['view']; ?>" /></a>
                        </th>
                    </tr>
    <?php endif;
endwhile; ?>
            <tr><td><a href="<?php echo HTTP_CTRL_MAIN . "?request=insert&table=$table" ?>">Nuovo</a></td></tr>
        </table>
<?php if ($l) : ?><a class="previous" href="<?php echo HTTP_CTRL_MAIN . "?table=$l&request=$request"; ?>"><div class="auto_margin" id="left-triangle"></div>Vai a <?php echo $l_label; ?></a><?php endif; ?>
<?php if ($r) : ?><a class="next" href="<?php echo HTTP_CTRL_MAIN . "?table=$r&request=$request"; ?>"><div class="auto_margin" id="right-triangle"></div>Vai a <?php echo $r_label; ?></a><?php endif; ?>
    </body>
</html>

